library(fixest)
library(readr)
library(jiebaR)
library(readr)
library(quanteda)
library(stm)
library(keyATM)
library(readxl)
library(stargazer)
library(haven)
library(mediation)
load("Data.RData")

#Figure A1. Effect of PRI_Refine on Expenditure
res_twfe = feols(Expenditure ~ i(Year, PRI_Refine,ref = c(2015))+PRI_Refine+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn| Firm + Year, data=Data)
summary(res_twfe)
iplot(res_twfe,main='Effect of PRI_Refine on Expenditure')

#Table B1. Descriptive Statistics
Describe<- subset(Data,select=c('Expenditure','PRI','PRI_Refine','Post2015','Sale','lgAge',
                                'TotalAsset','TaxRate','ROE','PoliConn','SOELevel','PollutionFirmYear',
                                'PRI_Subsidy','PRI_Investment','PRI_Financial','PRI_Environment',"PRI_IR",'EnvFineShare',
                                'Marketization','FixAsset','Loan','Finance','Private','PRIEnvHigh',
                                'RealEstate','ProvPlan','AveragePRI','EnvReg','PreExistingPC','Expenditure_Sale','Expenditure_Cost',
                                'PRI_Length','PRIHigh','PRIVeryHigh','PRI1Past5Year','TPAWebNews','TPANews','Attention','AllWebNews','AllNews'))
stargazer::stargazer(Describe)


#Table B2. Political Q&A by Sectors
View(IndData)

#Table C1. Topics and Keywords from STM Results (sorted by frequency) 
##Need to run the STM code for Figure 1

labelTopics(POstm10topic, n = 10) 
labelTopics(NPOstm10topic, n = 10) 

#Table D1. Keyword Assisted Topic Models
##Need to run KeyATM code for Table 4
top_words(Keyatm10, n = 5) 

#Table E1. Measurement Verification: DID Estimation of Anti-Corruption Campaign
m1<- feols(PRI~Prosecute|Year+SOELevel,cluster=~Firm,Data)
m2<- feols(PRI~AntiCorruption|Year+SOELevel,cluster=~Firm,Data)
m3<- feols(PRI_Refine~Prosecute|Year+SOELevel,cluster=~Firm,Data)
m4<- feols(PRI_Refine~AntiCorruption|Year+SOELevel,cluster=~Firm,Data)
etable(m1,m2,m3,m4,tex=TRUE,digits = 3,signif.code=c("***"=0.001, "**"=0.01, "*"=0.05,'$^{+}$'=0.1))

#Table E2. Measurement Verification 2: Inter- and Within-Sector Variation
v8<- feols(PRI_Refine~ProvPlan|Year+Prov+SOELevel,cluster=~Firm,Data)
v9<- feols(PRI_Refine~Finance|Year+Prov+SOELevel,cluster=~Firm,Data)
v10<- feols(PRI_Refine~RealEstate|Year+Prov+SOELevel,cluster=~Firm,Data)
v11<- feols(PRI_Refine~Marketization|Year+Sicmen+SOELevel,cluster=~Firm,Data)
v12<- feols(PRI_Refine~FixAsset|Year+Sicmen+SOELevel,cluster=~Firm,Data)
v13<- feols(PRI_Refine~Loan|Year+Sicmen+SOELevel,cluster=~Firm,Data)
v14<- feols(PRI_Refine~Marketization+FixAsset+Loan+Finance+RealEstate+ProvPlan|Year+SOELevel,cluster=~Firm,Data)
etable(v8,v9,v10,v11,v12,v13,v14,digits = 3,tex=TRUE,signif.code=c("***"=0.001, "**"=0.01, "*"=0.05,'+'=0.1))

#Table G1.Table G1. Alternative Measurements of PRI
m1<- feols(Expenditure~PRI_Length*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
m2<- feols(Expenditure~PRIHigh*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
m3<- feols(Expenditure~PRIVeryHigh*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
m4<- feols(Expenditure~Loan*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
m5<- feols(Expenditure~FixAsset*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
m6<- feols(Expenditure~Marketization*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
m7<- feols(Expenditure~PRI1Past5Year+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
etable(m1,m2,m3,m4,m5,m6,m7,tex=TRUE,digits = 3,signif.code=c("***"=0.001, "**"=0.01, "*"=0.05,'+'=0.1))

#Table G2. Alternative Measurements of Firm Responses
m1<- feols(Attention~PRI*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
m2<- feols(log(TPAWebNews+1)~PRI*Post2015+log(AllWebNews+1)+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
m3<- feols(log(TPANews+1)~PRI*Post2015+log(AllNews+1)+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
m4<- feols(Expenditure_Sale~PRI*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
m5<- feols(Expenditure_Cost~PRI*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
etable(m1,m2,m3,m4,m5,tex=TRUE,digits = 3,signif.code=c("***"=0.001, "**"=0.01, "*"=0.05,'+'=0.1))

#Table G3. Alternative Calculations of PRI and Subset Analysis
m1<- feols(Expenditure~log(PRI+1)*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
m2<- feols(Expenditure~Political*Post2015+All*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
m3<- feols(Expenditure~log(Political+1)*Post2015+log(All+1)*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
m4<- feols(Expenditure~PRI*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,subset(Data,37>All))
m5<- feols(Expenditure~PRI*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,subset(Data,All>12))
m6<- feols(Expenditure~PRI*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,subset(Data,All<=12))
etable(m1,m2,m3,m4,m5,m6,digits = 3,tex = TRUE,signif.code=c("***"=0.001, "**"=0.01, "*"=0.05,'+'=0.1))


#Table G4. Alternative Specifications
m1<- feols(Expenditure~PRI*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Prov+Year,cluster= ~Firm,Data)
m2<- feols(Expenditure~PRI*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Indcd+Year,cluster= ~Firm,Data)
m3<- feols(Expenditure~PRI*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+ProvYear,cluster= ~Firm,Data)
m4<- feols(Expenditure~PRI*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+IndYear,cluster= ~Firm,Data)
m5<- feols(Expenditure~PRI*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Prov,Data)
m6<- feols(Expenditure~PRI*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Indcd,Data)
etable(m1,m2,m3,m4,m5,m6,tex=TRUE,signif.code=c("***"=0.001, "**"=0.01, "*"=0.05,'+'=0.1))

#Table G5. PRI and CSR Rating
m1<- feols(indrate~PRI*Post2015+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,Data)
m2<- feols(indrate~PRI+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Firm+Year,cluster= ~Firm,subset(Data,Post2015==1))
etable(m1,m2,digits = 3,tex = TRUE,signif.code=c("***"=0.001, "**"=0.01, "*"=0.05,'+'=0.1))

#Table G6. PRI and Poverty Alleviation Expenditure: State-owned Enterprises
m1<- feols(Expenditure~PRI+PoliConn+Sale+TotalAsset+TaxRate+ROE+lgAge|Firm+Year,cluster= ~Firm,subset(Data,SOELevel!='Private'&Post2015==1))
m2<- feols(Expenditure~PRI*PoliConn+Sale+TotalAsset+TaxRate+ROE+lgAge|Firm+Year,cluster= ~Firm,subset(Data,SOELevel!='Private'&Post2015==1))
etable(m1,m2,digits = 3,tex = TRUE,signif.code=c("***"=0.001, "**"=0.01, "*"=0.05,'+'=0.1))

#Table G7. Causal Mediation Analysis: Quasi-Bayesian Confidence Intervals
Datasub<- subset(Data,Post2015==1&is.na(PRI)==0&
                   is.na(Sale)==0&
                   is.na(TotalAsset)==0&
                   is.na(TaxRate)==0&
                   is.na(ROE)==0&
                   is.na(lgAge)==0&
                   is.na(PoliConn)==0&
                   is.na(Firm)==0&
                   is.na(Year)==0&
                   is.na(EnvFineShare)==0)
#Treatment1
mm<- lm(Expenditure~PRI+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn+Firm+Year,data=Datasub)
my<- lm(EnvFineShare~PRI+Expenditure+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn+Firm+Year,data=Datasub)
Mediation_all <- mediate(mm, my, sims=1000, treat="PRI",mediator="Expenditure",group.out=Datasub$Firm)

#Treatment2
mm <- lm(Expenditure~PRI_Environment+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn+Firm+Year, data=Datasub)
my <- lm(EnvFineShare~PRI_Environment+Expenditure+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn+Firm+Year, data=Datasub)
Mediation_env <- mediate(mm, my, sims=1000, treat="PRI_Environment", mediator="Expenditure",group.out=Datasub$Firm)

#Treatment3
mm <- lm(Expenditure~PRI_Subsidy+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn+Firm+Year, data=Datasub)
my <- lm(EnvFineShare~PRI_Subsidy+Expenditure+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn+Firm+Year, data=Datasub)
Mediation_subsidy <- mediate(mm, my, sims=1000, treat="PRI_Subsidy", mediator="Expenditure",group.out=Datasub$Firm)   

#Treatment4
mm <- lm(Expenditure~PRI_Investment+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn+Firm+Year, data=Datasub)
my <- lm(EnvFineShare~PRI_Investment+Expenditure+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn+Firm+Year, data=Datasub)
Mediation_investment <- mediate(mm, my, sims=1000, treat="PRI_Investment", mediator="Expenditure",group.out=Datasub$Firm)   

#Treatment5
mm <- lm(Expenditure~PRI_Financial+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn+Firm+Year, data=Datasub)
my <- lm(EnvFineShare~PRI_Financial+Expenditure+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn+Firm+Year, data=Datasub)
Mediation_financial<- mediate(mm, my, sims=1000, treat="PRI_Financial", mediator="Expenditure",group.out=Datasub$Firm)

#Table G8. Poverty-alleviation Spending and Commerical Lawsuits: Logit Regression
Lawsuit<- subset(Lawsuit,select=c('Win','Expenditure','Sale','TotalAsset','TaxRate','ROE','lgAge','PoliConn','Year','Sicmen','Firm'))
m1<- femlm(Win~Expenditure,cluster=~Firm,family='logit',Lawsuit)
m2<- femlm(Win~Expenditure|Year+Sicmen,cluster=~Firm,family='logit',Lawsuit)
m3<- femlm(Win~Expenditure+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Year+Sicmen,cluster=~Firm,family='logit',Lawsuit)
m4<- femlm(Win~as.factor(Expenditure>0)+Sale+TotalAsset+TaxRate+ROE+lgAge+PoliConn|Year+Sicmen,cluster=~Firm,family='logit',Lawsuit)
etable(m1,m2,m3,m4,digits = 3,tex = TRUE,signif.code=c("***"=0.001, "**"=0.01, "*"=0.05,'+'=0.1))


